/*
 * @Date: 2021-05-25 21:05:12
 * @LastEditors: kanoyami
 * @LastEditTime: 2021-05-28 23:22:26
 */
const _ = require('lodash');
const config = require('../../config/config.json')
class SuperChart {
    SuperChartIntervalList = [];
    interValMap = {}

    genSuperChart(danmaku) {
        const id = String(danmaku.userInfo.userId.toNumber())
        if (_.indexOf(this.SuperChartIntervalList, id) >= 0) {
            if (!this.interValMap[id]) return null;
            _.remove(this.SuperChartIntervalList, function (n) {
                return n === id;
            });
            clearTimeout(this.interValMap[id].timeout)
            return { ...this.interValMap[id], content: danmaku.content }
        }
        return null;
    }

    genSCinterVal(liveGift) {
        const valueGate = process.env.NODE_ENV === 'development' ? 1 : config.scGate
        const duration =  process.env.NODE_ENV === 'development' ? 60000 :  liveGift.value
        const value =  process.env.NODE_ENV === 'development' ? 990000 :  liveGift.value
        if (liveGift.value >= valueGate) {
            const { userId, avatar, nickname } = liveGift.user;
            const id = String(userId.toNumber());
            const avatarUrl = avatar[0] ? avatar[0]["url"] : "";
            this.SuperChartIntervalList.push(id);

            this.interValMap[id] = {
                timeout: setTimeout(() => {
                    _.remove(this.SuperChartIntervalList, function (n) {
                        return n === id;
                    });
                    //_.remove(SuperChartIntervalList, id)
                    this.interValMap[id] = null;
                }, 30000),
                price: value / 10000,
                avatarUrl,
                authorName: nickname,
                duration,
            }
        }
    }
}



module.exports = SuperChart